package com.srwl.mytx.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;

import com.bm.library.Info;
import com.bm.library.PhotoView;
import com.srwl.mytx.Helper;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.Message;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by dahai2070 on 2018/3/25 0025.
 */

public class ShowBigImageViewPagerAdapter extends PagerAdapter {
    private static final String TAG = "ShowBigImageViewPagerAd";
    private List<Message> imageMessageList;
    private Context context;
    private OnHideBigImageListener hideBigImageListener;
    private String myId;
    private Map<Integer, Info> infoMap = new HashMap<>();
    private boolean firstShow = true;

    public ShowBigImageViewPagerAdapter(List<Message> imageMessageList, Context context, OnHideBigImageListener listener, String myId) {
        this.imageMessageList = imageMessageList;
        this.context = context;
        this.hideBigImageListener = listener;
        this.myId = myId;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return imageMessageList.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        View view = LayoutInflater.from(context).inflate(R.layout.tx_item_big_picture, null);
        final TextView titleTextView = view.findViewById(R.id.tv_picture_count);
        titleTextView.setText(position + 1 + "/" + imageMessageList.size());
        final PhotoView photoView = view.findViewById(R.id.iv_goods_picture);
        //开启缩放功能
        photoView.enable();
        photoView.setAnimaDuring(400);
        if (firstShow) {
            photoView.setVisibility(View.INVISIBLE);
        }
        ProgressBar progressBar = view.findViewById(R.id.progressBar);
        Message message = imageMessageList.get(position);
        String imageKey = message.getContent();
        String localPath;
        if (Utils.isSendMessage(message)) {
            //发出的消息，content 是完整的本地文件路径
            localPath = imageKey;
        } else {
            //收到的消息，content是 图片 key(图片名字)
            localPath = Helper.getInstance().getFilePath() + File.separator + imageKey;
        }
        //Glide.with(context).load(localPath).diskCacheStrategy(DiskCacheStrategy.ALL).into(photoView);
        GlideLoadUtils.glideLoad(context,localPath,photoView);
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                photoView.setOnClickListener(null);
                if (hideBigImageListener != null && !hideBigImageListener.beforeHideBigImage(position)) {
                    titleTextView.setVisibility(View.INVISIBLE);
                    bigImageAnimateOut(position, photoView);
                }
            }
        });
        photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        if (firstShow && infoMap.containsKey(position)) {
            firstShow = false;
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    photoView.setVisibility(View.VISIBLE);
                    photoView.animaFrom(infoMap.get(position));
                }
            }, 100);
//            photoView.setVisibility(View.VISIBLE);
//            photoView.animaFrom(infoMap.get(position));
        } else {
            photoView.setVisibility(View.VISIBLE);
        }
        container.addView(view);
        return view;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    public void bigImageAnimateOut(int position, final PhotoView photoView) {

        if (infoMap.containsKey(position)) {
            photoView.animaTo(infoMap.get(position), new Runnable() {
                @Override
                public void run() {
                    reSet();
                }
            });
        } else {
            Log.e(TAG, "bigImageAnimateOut: 没有找到对应的小图信息 position：" + position);

//            photoView.animaTo(null, new Runnable() {
//                @Override
//                public void run() {
//                    reSet();
//                }
//            });
        }

    }

    public interface OnHideBigImageListener {
        boolean beforeHideBigImage(int position);

        void showAnima();
    }

    public void setInfoMap(Map<Integer, Info> infoMap) {
        this.infoMap.putAll(infoMap);
    }

    public void setInfoMap(int position, Info info) {
        this.infoMap.put(position, info);
    }

    /**
     * 清除小图rect位置信息
     */
    private void reSet() {
        infoMap.clear();
        firstShow = true;
    }

}
